package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.CurrentAffairsDao;
import com.sc.nft.entity.CurrentAffairs;
import com.sc.nft.entity.dto.CurrentAffairsAddDTO;
import com.sc.nft.entity.dto.CurrentAffairsAdminDTO;
import com.sc.nft.entity.dto.CurrentAffairsTypeDTO;
import com.sc.nft.entity.vo.CurrentAffairsAdminVO;
import com.sc.nft.entity.vo.CurrentAffairsDetailVo;
import com.sc.nft.entity.vo.CurrentAffairsDetailsVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.CurrentAffairsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 时政要闻表服务接口实现
 *
 * @author zhangchao
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-03-05 10:52:52
 */
@Slf4j
@RequiredArgsConstructor
@Service
@RefreshScope
public class CurrentAffairsServiceImpl implements CurrentAffairsService {
    private final CurrentAffairsDao currentAffairsDao;

    @Value("${recommended.position.recommendedPositionNumber}")
    private int recommendedPositionNumber;


    @Override
    public Page<CurrentAffairsAdminVO> adminCurrentAffairsVoPage(CurrentAffairsAdminDTO currentAffairsAdminDTO) {
        return currentAffairsDao.adminCurrentAffairsVoPage(currentAffairsAdminDTO.toMp(), currentAffairsAdminDTO.getCurrentAffairsTypeId(), currentAffairsAdminDTO.getArticleName());
    }

    @Override
    public void currentAffairsAdd(CurrentAffairsAddDTO currentAffairsAddDTO) {
        CurrentAffairs currentAffairs = BeanUtil.copyProperties(currentAffairsAddDTO, CurrentAffairs.class);
        Assert.isTrue(currentAffairs.insert(),()->new GlobalRunTimeException("新增失败！"));
    }

    @Override
    public CurrentAffairsDetailsVO currentAffairsAdminDetails(Long id) {
        return currentAffairsDao.currentAffairsAdminDetails(id);
    }

    @Override
    public CurrentAffairsDetailVo currentAffairsDetails(Long id) {
        return currentAffairsDao.currentAffairsDetails(id);
    }

    @Override
    public void currentAffairsUpdate(CurrentAffairs currentAffairs) {
        CurrentAffairs currentAffairsDaoById = currentAffairsDao.getById(currentAffairs.getId());
        Assert.isFalse(Boolean.TRUE.equals(currentAffairsDaoById.getIsRecommend()), () -> new GlobalRunTimeException("上架的资讯不支持修改！"));
        Assert.isTrue(currentAffairsDao.currentAffairsUpdate(currentAffairs),()->new GlobalRunTimeException("更新失败！"));
    }

    @Override
    public void currentAffairsDelete(Long id) {
        CurrentAffairs currentAffairsDaoById = currentAffairsDao.getById(id);
        Assert.isFalse(Boolean.TRUE.equals(currentAffairsDaoById.getIsRecommend()), () -> new GlobalRunTimeException("上架的资讯不支持删除！"));
        Assert.isTrue(currentAffairsDao.currentAffairsDelete(id),()->new GlobalRunTimeException("删除失败！"));
    }

    @Override
    public void currentAffairsShow(Long id, Boolean switchRequest) {
        CurrentAffairs currentAffairsDaoById = currentAffairsDao.getById(id);
        Assert.isTrue(ObjectUtil.isNotNull(currentAffairsDaoById), () -> new GlobalRunTimeException("该资讯不存在！"));
        Assert.isTrue(currentAffairsDao.currentAffairsShow(id, switchRequest), () -> new GlobalRunTimeException("更新失败！"));
    }

    @Override
    public void currentAffairsRecommend(Long id, Boolean switchRequest) {
        CurrentAffairs currentAffairsDaoById = currentAffairsDao.getById(id);
        List<CurrentAffairsDetailsVO> recommendCount = currentAffairsDao.getCurrentAffairsRecommend(currentAffairsDaoById.getCurrentAffairsTypeId());
        Assert.isFalse(switchRequest && ObjectUtil.isNotNull(recommendCount) && recommendCount.size() >= recommendedPositionNumber, () -> new GlobalRunTimeException("推荐位已满"));
        Assert.isTrue(currentAffairsDao.currentAffairsRecommend(id, switchRequest), () -> new GlobalRunTimeException("更新失败！"));
    }

    @Override
    public Page<CurrentAffairsDetailsVO> getCurrentAffairsRecommendPage(CurrentAffairsTypeDTO currentAffairsTypeDTO) {
        return currentAffairsDao.getCurrentAffairsRecommendPage(currentAffairsTypeDTO);
    }


}